#    Copyright 2008, 2009 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.

#************************BEGIN Add Include Files to Path************************
import sys
IncludePath="/opt/coopgis/djangoproject/include"
if(not IncludePath in sys.path):
    sys.path.append(IncludePath)
#************************END   Add Include Files to Path************************
from django.contrib.gis.geos import *
import os, time, psycopg2, datetime

#********************Setup psycopg2 and django section********************
PathDjangoRoot="/opt/coopgis/"
if(not PathDjangoRoot in sys.path):
    sys.path.append(PathDjangoRoot)
from django.core.management import setup_environ
import djangoproject.settings
setup_environ(djangoproject.settings)
from djangoproject.models import *
from django.contrib.gis.geos import GEOSGeometry
import COOPGIS_Ajax, COOPGIS_DynamicTemplate, COOPGIS_FormMisc, COOPGIS_GPS, COOPGIS_Misc, COOPGIS_ReportLab, COOPGIS_String, COOPGIS_Update
#********************Setup psycopg2 and django section********************

def q(BooleanValue):
    """Quick throw-away function to return */'' based on a boolean value"""
    if(BooleanValue):
        return '*'
    else:
        return ''

OutputFileName='/home/coopgisadmin/Desktop/units.csv'
a = CompatibleUnit.objects.all()

#a = CompatibleUnit.objects.all().exclude(StakerPoleIndicator=True)
#a = a.exclude(StakerTransformerIndicator=True).exclude(StakerMiscEquipmentIndicator=True).exclude(StakerGroundIndicator=True)
#a = a.exclude(StakerGuyGuyIndicator=True).exclude(StakerGuyAnchorIndicator=True).order_by('Name')


b = []
for i in a:
    Count = 0
    if(i.StakerPoleIndicator):
        Count = Count + 1
    if(i.StakerTransformerIndicator):
        Count = Count + 1
    if(i.StakerMiscEquipmentIndicator):
        Count = Count + 1
    if(i.StakerGroundIndicator):
        Count = Count + 1
    if(i.StakerGuyGuyIndicator):
        Count = Count + 1
    if(i.StakerGuyAnchorIndicator):
        Count = Count + 1
    if(Count > 1):
        b.append(i)



Headings=['Name', 'StakerPoleIndicator', 'StakerTransformerIndicator',
          'StakerMiscEquipmentIndicator', 'StakerGroundIndicator', 'StakerGuyGuyIndicator',
          'StakerGuyAnchorIndicator']
List = []

for i in b:
    List.append([i.Name, q(i.StakerPoleIndicator), q(i.StakerTransformerIndicator), q(i.StakerMiscEquipmentIndicator), 
                         q(i.StakerGroundIndicator), q(i.StakerGuyGuyIndicator), q(i.StakerGuyAnchorIndicator)])
try:
    os.remove(OutputFileName)
except:
    pass

import csv
File=open(OutputFileName, 'wb')
CSVWriter=csv.writer(File)
CSVWriter.writerow(Headings)
CSVWriter.writerows(List)
File.close()



